package rad.framework.mail;

import static org.jboss.seam.ScopeType.APPLICATION;
import static org.jboss.seam.annotations.Install.BUILT_IN;

import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;

import com.dumbster.smtp.SimpleSmtpServer;

/**
 * Initialize the Dumbster mock smtp server for unit testing mail components.
 * 
 * http://quintanasoft.com/dumbster/
 * 
 * @author boudyacho
 * 
 */
@Name("org.jboss.seam.mail.dumbster")
@Startup
@Scope(APPLICATION)
@BypassInterceptors
@Install(precedence = BUILT_IN, classDependencies = "com.dumbster.smtp.SimpleSmtpServer")
public class Dumbster {

	@Logger
	private Log log;

	private Integer port = 24; // must match MailSession

	private SimpleSmtpServer server;

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public SimpleSmtpServer getServer() {
		return server;
	}

	@Create
	public void start() throws Exception {
		log.debug("Starting Dumbster: #0", getPort());
		server = SimpleSmtpServer.start(getPort());
	}

	public void stop() {
		log.debug("Stoping Dumbster");
		server.stop();
	}
}
